package com.itcpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcpeng.constant.MessageConstant;
import com.itcpeng.constant.RedisMessageConstant;
import com.itcpeng.entity.Result;
import com.itcpeng.pojo.Member;
import com.itcpeng.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping(value = "/login")
public class LoginController {

    @Reference
    private MemberService memberService;

    @Autowired
    private JedisPool jedisPool;

    @PostMapping(value = "/check")
    public Result check(@RequestBody Map<String, String> map , HttpServletResponse response) {
        //验证验证码
        Jedis jedis = jedisPool.getResource();
        String telephone = map.get("telephone");
        String key = "login_" + RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
        //jedis生成验证码
        String s = jedis.get(key);
        if (StringUtils.isEmpty(s)) {
            //空值或者没有验证码
            return new Result(false, MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        //有值  判断是否正确
        if (!s.equals(map.get("validateCode"))) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        //清除验证码
        jedis.del(key);
        //判断会员是否存在
        Member member = memberService.findByTelephone(telephone);
        //会员不存在
        if (member == null) {
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberService.add(member);
        }
        //跟踪用户行踪
        Cookie cookie = new Cookie("login_member_telephone", telephone);
        //有效期为一个月
        cookie.setMaxAge(60 * 60 * 24 * 30);
        //访问哪个页面时带上cookie  / 表示根目录
        cookie.setPath("/");
        response.addCookie(cookie);
        return new Result(true, MessageConstant.LOGIN_SUCCESS);

    }

    @PostMapping(value = "/findTelephone")
    public Result findTelephone(@RequestBody Map<String, String> map , HttpServletResponse response) {
        //验证验证码
        Jedis jedis = jedisPool.getResource();
        String telephone = map.get("telephone");
        String key = "login_" + RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
        //jedis生成验证码
        String s = jedis.get(key);
        if (StringUtils.isEmpty(s)) {
            //空值或者没有验证码
            return new Result(false, MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        //有值  判断是否正确
        if (!s.equals(map.get("validateCode"))) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        //清除验证码
        jedis.del(key);
        //判断会员是否存在
        Member member = memberService.findByTelephone(telephone);
        //会员不存在
        if (member == null) {
            return new Result(false, MessageConstant.GET_MEMBER_QUERY_FAIL);
        }
        //跟踪用户行踪
        Cookie cookie = new Cookie("login_member_telephone", telephone);
        //有效期为一个月
        cookie.setMaxAge(60 * 60 * 24 * 30);
        //访问哪个页面时带上cookie  / 表示根目录
        cookie.setPath("/");
        response.addCookie(cookie);
        return new Result(true, MessageConstant.LOGIN_SUCCESS,map);

    }

}
